'use strict';
const dayjs = require("dayjs");
const getNumberValue = function (value) {
    let numberValue = Number(value);
    return isNaN(numberValue) ? 0 : numberValue;
}
module.exports = app => {
  const { STRING, INTEGER, JSON, DATE, DECIMAL, TEXT } = app.Sequelize;
  const Order = app.model.define(
      'Order',
      {
            id: {type: INTEGER, primaryKey: true},
            ship_id: INTEGER,
            cycle_id: INTEGER,
            count: {
                type: DECIMAL(10,1),
                // 返回格式化为数字
                get() {
                   return getNumberValue(this.getDataValue('count'));
                }
            },
            status: STRING, // pending 进行中 confirmed 已确认
            pay_way: STRING(255), // 支付方式 paypal ali
            paypal_order_id:STRING(255),
            payment_status:STRING(255), // paid
            total_price: {
                type: DECIMAL(10, 2),
                // 返回格式化为数字
                get() {
                   return getNumberValue(this.getDataValue('total_price'));
                }
            }, // 总价
          // 优惠金额
            discount_amount: {
                type: DECIMAL(10, 2),
                // 返回格式化为数字
                get() {
                   return getNumberValue(this.getDataValue('discount_amount'));
                }
            },
           // 优惠卷
            coupon_id: INTEGER,
            amount: {
                type: DECIMAL(10, 2),
                // 返回格式化为数字
                get() {
                   return getNumberValue(this.getDataValue('amount'));
                }
            }, // 实付款
            adult_count: INTEGER,
            child_count: INTEGER,
            childs_count: INTEGER,
              create_time: {
                  type: DATE,
                  get() {
                      return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
                  }
              },
            order_no: STRING(255),
            pay_time: {
                type: DATE,
                get() {
                    return dayjs(this.getDataValue('pay_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            },
            status_value: STRING(255),
            status_name: STRING(255),
            role: INTEGER(11), // 1 普通用户 2 代理商
            user_id: INTEGER,
            delete_flag: INTEGER,
            // 联系人
            contact_name: TEXT,
            contact_phone: TEXT,
            contact_email: TEXT,
            contact_wx: TEXT,
            contact_remark: TEXT,
            pay_detail: JSON,
            paypal_capture_id: STRING(255),
            refund_remark: TEXT,
            refund_time: {
                type: DATE,
                get() {
                    return dayjs(this.getDataValue('refund_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            },
            refund_amount: {
                type: DECIMAL(10, 2),
                // 返回格式化为数字
                get() {
                   return getNumberValue(this.getDataValue('refund_amount'));
                }
            }, // 实付款
            refund_id: STRING(255),
            alipay_payment_id: STRING(255),
            alipay_create_detail: JSON,
            alipay_success_detail: JSON,
            alipay_capture_detail: JSON,
            alipay_capture_success_detail: JSON,
          alipay_refund_detail: JSON,
          alipay_refund_success_detail: JSON,
            amount_status: INTEGER,
            amount_status_name: STRING(255),
              eat_remark:TEXT,
              is_diving: INTEGER,
              has_special_needs: INTEGER,
              needs_confirmation: INTEGER,
              source: STRING,
                diving_file: TEXT,
          ip: STRING,
          country: STRING,
          lang: STRING,
          start_time: STRING,
          end_time: STRING,

          duration: {
              type: DECIMAL(10,1),
              get() {
                  return getNumberValue(this.getDataValue('duration'))
              }
          },
      },
      {
          tableName: 'order',
          timestamps: true, // 启用时间戳(createdAt, updatedAt)
          updatedAt: 'updated_time', // 不想要 updatedAt
          createdAt: false, // 想要  createdAt 但是希望名称叫做 created_at
      }
  );
    Order.associate = function() {
        // 关联ship表
        app.model.Order.belongsTo(app.model.Ship, { foreignKey: 'ship_id', targetKey: 'id', as: 'ship' });
        // 关联cycle表
        app.model.Order.belongsTo(app.model.Cycle, { foreignKey: 'cycle_id', targetKey: 'id', as: 'cycle' });
        // 关联coupon表
        app.model.Order.belongsTo(app.model.Coupon, { foreignKey: 'coupon_id', targetKey: 'id', as: 'coupon' });
        // 用户
        app.model.Order.belongsTo(app.model.User, { foreignKey: 'user_id', targetKey: 'id', as: 'user' });
        // 一对多orderUser
        app.model.Order.hasMany(app.model.OrderUser, { foreignKey: 'order_id', targetKey: 'id', as: 'userList' });
    }
  return Order
};

